<?php
/**
 * phpcr
 *
 * Copyright (c) 2009, Olivier Hoareau <hoareau.olivier@gmail.com>.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 *
 *   * Neither the name of Olivier Hoareau nor the names of his
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * @package   phpcr
 * @author    Olivier Hoareau <hoareau.olivier@gmail.com>
 * @copyright 2009 Olivier Hoareau <hoareau.olivier@gmail.com>
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
 * @since     File available since Release 1.0.0
 */

/**
 * PHPCR code analyser.
 *
 * @author    Johann-Peter Hartmann <johann-peter.hartmann@mayflower.de>
 * @author    Sebastian Bergmann <sb@sebastian-bergmann.de>
 * @author    Olivier Hoareau <hoareau.olivier@gmail.com>
 * @copyright 2009 Olivier Hoareau <hoareau.olivier@gmail.com>
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
 * @version   Release: @package_version@
 * @link      http://code.google.com/p/phpcoderatio
 * @since     Class available since Release 0.1.0
 */
class PHPCR_Analyzer
{
    /**
     * Code ration computer (CR).
     *
     * @param  Iterator|array   $files     List of files to process
     * @param  array            $patterns  List of patterns to dispatch statistics in (based on file name)
     * 
     * @author Johann-Peter Hartmann <johann-peter.hartmann@mayflower.de>
     * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
     * 
     * @return array
     */
    public static function analyze($files,$patterns=array())
    {
        $results = array('files'=>array(),'comment'=>array('nb'=>0,'size'=>0),'whitespace'=>array('nb'=>0,'size'=>0),'html'=>array('nb'=>0,'size'=>0),'code'=>array('nb'=>0,'size'=>0),'ratio'=>array(),'size'=>array('patterns'=>array(),'total'=>0),'count'=>array('patterns'=>array(),'total'=>0),'lines'=>array('patterns'=>array(),'total'=>0));
        foreach($files as $fileInfo) {
            $file = (string)$fileInfo;
            if (false === isset($results['files'][$file])) {
                $results['files'][$file] = array('comment'=>array('nb'=>0,'size'=>0),'whitespace'=>array('nb'=>0,'size'=>0),'html'=>array('nb'=>0,'size'=>0),'code'=>array('nb'=>0,'size'=>0),'ratio'=>array(),'size'=>0,'lines'=>0);
            }
            foreach(token_get_all(file_get_contents($file)) as $token) {
                if (is_array($token)) {
                    switch($token[0]) {
                        case defined('T_ML_COMMENT') ? T_ML_COMMENT : 'T_ML_COMMENT':
                        case T_DOC_COMMENT:
                        case T_COMMENT:
                            $size = strlen($token[1]);
                            $results['comment']['nb']++;
                            $results['comment']['size']+=$size;
                            $results['files'][$file]['comment']['nb']++;
                            $results['files'][$file]['comment']['size']+=$size;
                            break;
                        case T_ENCAPSED_AND_WHITESPACE:
                        case T_WHITESPACE:
                            $size = strlen($token[1]);
                            $results['whitespace']['nb']++;
                            $results['whitespace']['size']+=$size;
                            $results['files'][$file]['whitespace']['nb']++;
                            $results['files'][$file]['whitespace']['size']+=$size;
                            break;
                        case T_INLINE_HTML:
                            $size = strlen($token[1]);
                            $results['html']['nb']++;
                            $results['html']['size']+=$size;
                            $results['files'][$file]['html']['nb']++;
                            $results['files'][$file]['html']['size']+=$size;
                            break;
                        default:
                            $size = strlen($token[1]);
                            $results['code']['size']+=$size;
                            $results['files'][$file]['code']['nb']++;
                            $results['files'][$file]['code']['size']+=$size;
                            break;
                    }
                } else {
                    $size = strlen($token);
                    $results['code']['size']+=$size;
                    $results['files'][$file]['code']['size']+=$size;
                }
            }
            $results['files'][$file]['ratio']['code'] = (float)$results['files'][$file]['code']['size'] / ($results['files'][$file]['code']['size']+$results['files'][$file]['comment']['size']+$results['files'][$file]['html']['size']+$results['files'][$file]['whitespace']['size']);
            $results['files'][$file]['ratio']['code-with-space'] = (float)($results['files'][$file]['code']['size']+$results['files'][$file]['whitespace']['size']) / ($results['files'][$file]['code']['size']+$results['files'][$file]['comment']['size']+$results['files'][$file]['html']['size']+$results['files'][$file]['whitespace']['size']);
            $results['files'][$file]['ratio']['code-with-space-and-html'] = (float)($results['files'][$file]['code']['size']+$results['files'][$file]['whitespace']['size']+$results['files'][$file]['html']['size']) / ($results['files'][$file]['code']['size']+$results['files'][$file]['comment']['size']+$results['files'][$file]['html']['size']+$results['files'][$file]['whitespace']['size']);
            $results['files'][$file]['ratio']['comment'] = (float)($results['files'][$file]['comment']['size']) / ($results['files'][$file]['code']['size']+$results['files'][$file]['comment']['size']+$results['files'][$file]['html']['size']+$results['files'][$file]['whitespace']['size']);
            $results['files'][$file]['ratio']['html'] = (float)($results['files'][$file]['html']['size']) / ($results['files'][$file]['code']['size']+$results['files'][$file]['comment']['size']+$results['files'][$file]['html']['size']+$results['files'][$file]['whitespace']['size']);
            $results['files'][$file]['ratio']['whitespace'] = (float)($results['files'][$file]['whitespace']['size']) / ($results['files'][$file]['code']['size']+$results['files'][$file]['comment']['size']+$results['files'][$file]['html']['size']+$results['files'][$file]['whitespace']['size']);
            
            // file information
            $results['files'][$file]['size'] = $fileInfo->getSize();
            $results['files'][$file]['lines'] = count(file($fileInfo->getRealPath()));
            $results['size']['total'] += $results['files'][$file]['size'];
            $results['lines']['total'] += $results['files'][$file]['lines'];
            $results['count']['total'] += 1;
            foreach($patterns as $patternName=>$pattern) {
                $matches = null;
                if (preg_match("/$pattern/",$file,$matches)>0) {
                    if (false === isset($results['size']['patterns'][$patternName])) {
                        $results['size']['patterns'][$patternName] = 0;
                        $results['lines']['patterns'][$patternName] = 0;
                        $results['count']['patterns'][$patternName] = 0;
                    }
                    $results['size']['patterns'][$patternName]+=$results['files'][$file]['size'];
                    $results['lines']['patterns'][$patternName]+=$results['files'][$file]['lines'];
                    $results['count']['patterns'][$patternName]++;
                    break; // avoid counting file into more than one patternName
                }
            }
        }
        foreach($results['files'] as $file=>$data) {
            $results['files'][$file]['ratio']['size'] = (float)$data['size'] / $results['size']['total'];
            $results['files'][$file]['ratio']['lines'] = (float)$data['lines'] / $results['lines']['total'];
        }
        $results['ratio']['code'] = (float)$results['code']['size'] / ($results['code']['size']+$results['comment']['size']+$results['html']['size']+$results['whitespace']['size']);
        $results['ratio']['code-with-space'] = (float)($results['code']['size']+$results['whitespace']['size']) / ($results['code']['size']+$results['comment']['size']+$results['html']['size']+$results['whitespace']['size']);
        $results['ratio']['code-with-space-and-html'] = (float)($results['code']['size']+$results['whitespace']['size']+$results['html']['size']) / ($results['code']['size']+$results['comment']['size']+$results['html']['size']+$results['whitespace']['size']);
        $results['ratio']['comment'] = (float)($results['comment']['size']) / ($results['code']['size']+$results['comment']['size']+$results['html']['size']+$results['whitespace']['size']);
        $results['ratio']['html'] = (float)($results['html']['size']) / ($results['code']['size']+$results['comment']['size']+$results['html']['size']+$results['whitespace']['size']);
        $results['ratio']['whitespace'] = (float)($results['whitespace']['size']) / ($results['code']['size']+$results['comment']['size']+$results['html']['size']+$results['whitespace']['size']);
        
        foreach($results['size']['patterns'] as $patternName=>$value) {
            $results['ratio'][$patternName.'-size'] = (float)$value / $results['size']['total'];
        }
        foreach($results['count']['patterns'] as $patternName=>$value) {
            $results['ratio'][$patternName.'-count'] = (float)$value / $results['count']['total'];
        }
        foreach($results['lines']['patterns'] as $patternName=>$value) {
            $results['ratio'][$patternName.'-lines'] = (float)$value / $results['lines']['total'];
        }
        return $results;
    }
}
